//
//  COFollowListController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/10.
//

import UIKit

class COFollowListController: COBaseController,UITableViewDataSource,UITableViewDelegate {
    
    lazy var pageNum: Int = 1
    fileprivate var newAndHot = [HotListModel]()      // 数组
    
    lazy var baseTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        if #available(iOS 11.0, *) {
            if #available(iOS 13.0, *) {
                tableView.automaticallyAdjustsScrollIndicatorInsets = false
            }else{
                tableView.contentInsetAdjustmentBehavior = .never
            }
        }else {
            automaticallyAdjustsScrollViewInsets = false
        }
        return tableView
    }()

    lazy var containView: UIView = {
        let con = UIView()
        con.backgroundColor = .white
        return con
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.addnav()
        self.title = "Following"
        setUI()
    }
    
    // 设置UI
    func setUI(){
        self.view.addSubview(self.containView)
        self.containView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.navView.snp.bottom)
        }
        self.containView.addSubview(self.baseTableView)
        self.baseTableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(PTW(10))
        }
        self.baseTableView.headerRefresh {[weak self] in
            self?.pageNum = 1
            self?.fetchData(true)
        }

        self.baseTableView.footerRefresh {[weak self] in
            self?.pageNum += 1
            self?.fetchData(false)
        }
        self.baseTableView.headerStartRefreshing()
        self.baseTableView.footerEndRefreshing(true)
    }
    
    func fetchData(_ isRefresh:Bool = false){
        var params = [String:Any]()
        params["pageNum"]  = self.pageNum
        params["pageSize"] = 30
        COHttpManager.request(target: COBaseApi.streamerFollowList(param: params), modelType: COHotResponseModel.self) {[weak self] code, object, success in
            let list = object?.pageData
            if isRefresh == true {
                self?.newAndHot = list ?? [HotListModel]()
                self?.baseTableView.headerEndRefreshing()
                self?.baseTableView.footerEndRefreshing((list?.count == 0) ? true : false)
                if list?.count ?? 0 > 0{
                    CONodataView.dg_hide((self?.baseTableView)!)
                }else {
                    CONodataView.dg_show((self?.baseTableView)!)
                }
            }else {
                if list?.count ?? 0 > 0 {
                    if let items = self?.newAndHot, let list = list{
                        self?.newAndHot = items + list
                    }
                }
                self?.baseTableView.footerEndRefreshing((list?.count == 0) ? true : false)
            }
            self?.baseTableView.reloadData()
        } failureBlock: {[weak self] code, msg in
            self?.baseTableView.headerEndRefreshing()
            self?.baseTableView.footerEndRefreshing()
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.containView.clipsCorners(corners: [.topLeft,.topRight], radius: PTW(30))
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.newAndHot.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = COFollowListViewCell().cellWithTableView(tableView)
        cell.selectionStyle = .none
        cell.model = self.newAndHot[indexPath.row]
        cell.avaterBack = { [weak self] in
            self?.avaterAction(indexPath: indexPath)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return PTW(70)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 取消关注
        let model = self.newAndHot[indexPath.row]
        var params = [String:Any]()
        params["streamerId"]  = model.id
        // 取消关注
        UIToolHelper.showHUD()
        COHttpManager.request(target: COBaseApi.streamerUnFollow(param: params), modelType: CONSObjectModel.self) {[weak self] code, object, success in
            UIToolHelper.hideHUD()
            UIToolHelper.makeToast("Sussessful!")
            self?.newAndHot.remove(at: indexPath.row)
            self?.baseTableView.deleteRows(at: [indexPath], with: .none)
            NotificationCenter.default.post(name: .FollowFinishNoti, object: nil)
        } failureBlock: { code, msg in
            UIToolHelper.hideHUD()
            UIToolHelper.makeToast("Network error,Please try again later!")
        }
    }
    
    func avaterAction(indexPath: IndexPath){
        let profile = COOtherInfoController()
        let model = self.newAndHot[indexPath.row]
        profile.userId = model.id
        self.navigationController?.pushViewController(profile, animated: true)
    }
}
